import { getUserInfoApi } from "@/api/user";
import { defineStore } from "pinia";
import { ref } from "vue";

const useUserStore = defineStore(
  "user",
  () => {
    // ref声明数据 相当于state中的数据
    // 声明方法   相当于actions中的方法
    // 声明计算属性computed 相当于getters
    const token = ref(null);
    const userInfo = ref({});

    const updateToken = (newToken) => {
      token.value = newToken;
    };
    const getUserInfo = async () => {
      const { data: res } = await getUserInfoApi();
      userInfo.value = res.data;
    };
    const delUserInfo = () => {
      userInfo.value = {};
    };

    return {
      token,
      updateToken,
      userInfo,
      getUserInfo,
      delUserInfo,
    };
  },
  {
    persist: true,
  }
);

export default useUserStore;
